package com.gmail.jakegibel.TLA;

import java.util.ArrayList;
import java.util.Collection;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Snowball;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

public class TLAPlayerListener implements Listener{

    Material FireBendMaterial = Material.LOG;
    private boolean holdingWater;
    private Block block;
    private Material WaterBendMaterial = Material.DIRT;
    //private Material EarthBendMaterial = Material.BRICK;
    private Material SnowBallMaterial = Material.SLIME_BALL;
    private Material SparkySparkyBoomMan = Material.RAW_FISH;
    //private int waterDistance = 3;
    private Material EarthMoveMaterial = Material.MINECART;
    public static TLA plugin;
    private Material AirBendMaterial = Material.STICK;
    private Collection<PotionEffect> effects = new ArrayList<PotionEffect>();
    
    public TLAPlayerListener(TLA tla) {
        plugin = tla;
        
        
    }
    @EventHandler
    public void onEntityDamageByEntity ( EntityDamageByEntityEvent event )
    {
        Entity damagedentity = event.getEntity();
        Entity damagerentity = event.getDamager();
             if( damagerentity instanceof Snowball )
        {
            LivingEntity ent = (LivingEntity) damagedentity;
            ent.damage(5);
        }
    }
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event){
        Player player = event.getPlayer();
        Material material = event.getMaterial();
        System.out.println("Material: " + material + " Event: " + event.getAction());
        if(material == FireBendMaterial && player.hasPermission("tla.fire.shoot")){//&& player.hasPermission("firebending")){
                
            plugin.shootFireball(player);
/**
        } else if (material == WaterBendMaterial && !holdingWater && (event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.LEFT_CLICK_AIR) && player.hasPermission("TLA.water.move")){
            //This is when you're a water bender and you want to move a block
            int i = 1;
            Material lookedAt = Material.AIR;
            Block blocTest = null;
            while (i <= 10 && !(lookedAt == Material.WATER || lookedAt == Material.STATIONARY_WATER)){
                blocTest = player.getTargetBlock(null, i);
                lookedAt = blocTest.getType();
                i++;
                System.out.println("Because you clicked: i: " + i +  " | " + lookedAt);
            }
            
            if(lookedAt == Material.WATER || lookedAt == Material.STATIONARY_WATER){
                waterDistance  = i;
                holdingWater = true;
                block = blocTest;
            }
        } else if ((event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.LEFT_CLICK_AIR) && holdingWater){
            //This is to drop the water 
            holdingWater = false;
            System.out.println("Dropped the water you were holding");
            */
        } else if ((event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR) && material == AirBendMaterial && player.hasPermission("TLA.air.blast")){
            //This will allow you to do a shockwave around the player
            System.out.println("AirBending");
            plugin.airBlast(player);
        } else if (material == SnowBallMaterial ){
            plugin.shootSnowBall(player);
        } else if (material == SparkySparkyBoomMan  ){
            plugin.shootExplosion(player);
        } else if (holdingWater && material == WaterBendMaterial){
            plugin.switchIce(block);
        } else if (material == EarthMoveMaterial && event.getAction() == Action.LEFT_CLICK_BLOCK){
            plugin.pushEarth(player, event.getClickedBlock(), event.getBlockFace());
        } else if (material == EarthMoveMaterial && event.getAction() == Action.RIGHT_CLICK_BLOCK){
            plugin.pullEarth(player, event.getClickedBlock(), event.getBlockFace());
        } else if (material == AirBendMaterial && player.hasPermission("TLA.air.jump") && (event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.LEFT_CLICK_AIR)){
            plugin.toggleFlight(player);
        }
    }
    @EventHandler
    public void onPlayerDamage(EntityDamageEvent e){
        if(e.getEntity() instanceof Player){
            if(e.getCause() == DamageCause.FALL && ((Player) e.getEntity()).hasPermission("TLA.air.jump")){
                
                e.setCancelled(true);
                
            }
        }
    }
    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event){
        Player player = event.getPlayer();
        //System.out.println(player.getEffectivePermissions());
        //System.out.println(player.isPermissionSet("TLA.fire.shoot"));
        if(player.hasPermission("TLA.fire.resistance") && effects.size() < 5){
            effects.add(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 1000, 3));
        }
        if(player.hasPermission("TLA.air.jump") && effects.size() < 5){
            effects.add(new PotionEffect(PotionEffectType.JUMP, 1000, 3));
        }
        if(player.hasPermission("TLA.water.move") && effects.size() < 5){
            effects.add(new PotionEffect(PotionEffectType.WATER_BREATHING, 1000, 3));
        }
        if(player.hasPermission("TLA.air.speed") && effects.size() < 5){
            effects.add(new PotionEffect(PotionEffectType.SPEED, 1000, 3));
        }
        if(holdingWater){//&& player.hasPermission("firebending")){
            /**
            //while(event.getMaterial() == FireBendMaterial){
            Object[] result = plugin.moveWater(player, block, waterDistance);
            block = (Block) result[0];
            waterDistance = (Integer) result[1];
            //block = plugin.moveWater(player, block, waterDistance);
            //Block newBlock = 
            //block.setType(Material.AIR);    
            //plugin.shootFireball(player);
            //}
             
             */
        }
        
        if(player.getActivePotionEffects().size() == 0){
            
            player.addPotionEffects(effects);
            effects.clear();
        }
    }
}
